﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Configuration;
using DWH.Utils;
using DWH.Data;
using System.Text.RegularExpressions;
using DWH.Configuration;
using DWH.PP;
using System.IO;
using System.Diagnostics;
using System.Threading.Tasks;

namespace RDTools
{
    public partial class RDTools : Form
    {
        public RDTools()
        {
            InitializeComponent();
            initializeSettings();
        }

        private void initializeSettings()
        {
            txtAttFolder.Text = ConfigurationManager.AppSettings[DWHConstants.KEY_WEB_ATTACHMENT_STORE_FOLDER];
            txtDWHDB.Text = ConfigurationManager.ConnectionStrings["DWHDBEntities"].ConnectionString;

            List<RDTaskConfiguration> configs = (List<RDTaskConfiguration>)ConfigurationManager.GetSection("RDConfiguration");

            var q = from c in configs select c;

            cbRDTaskList.DisplayMember = "TaskName";

            cbRDTaskList.Items.AddRange(q.ToArray<RDTaskConfiguration>());
        }

        private void onTxtURLChange(object sender, EventArgs e)
        {
            if (!String.IsNullOrEmpty(txtURL.Text))
            {
                txtURLHashCode.Text = txtURL.Text.GetHashCode().ToString();
            }
        }

        private Page currentPage;

        private void onBtnValidateClick(object sender, EventArgs e)
        {
            updateStatusBarText("pulling data from database");

            long urlHashCode;

            if (!String.IsNullOrEmpty(txtURLHashCode.Text) && Int64.TryParse(txtURLHashCode.Text, out urlHashCode))
            {
                Page p = DWHDBHelper.GetPageByHashCode(urlHashCode);
                if (p != null)
                {
                    updateStatusBarText("this page has been pulled and stored in database");
                    currentPage = p;
                }
                else
                {
                    updateStatusBarText("this page has NOT been pulled");
                    ckPullToDB.Checked = true;
                }
            }
            else
            {
                updateStatusBarText("no valid url hash code");                
            }            
        }

        private void onBtnPullPushClick(object sender, EventArgs e)
        {
            RDTaskConfiguration config = (RDTaskConfiguration)cbRDTaskList.SelectedItem;
            PPBoardPullTaskBase task = (PPBoardPullTaskBase)PPTaskExecutor.CreatePPTaskFromRDTaskConfiguration(config);

            if (ckPullToDB.Checked)
            {
                updateStatusBarText("try to pull page from internet to db");                  
                                
                task.ProcessPageContent(txtURL.Text);

                currentPage = DWHDBHelper.GetPageByHashCode(txtURL.Text.GetHashCode());

                updateStatusBarText("finish pull page from internet to db");
            }

            if (currentPage == null)
            {
                currentPage = DWHDBHelper.GetPageByHashCode(txtURL.Text.GetHashCode());
            }

            if (currentPage == null)
            {
                updateStatusBarText("please pull the page from internet first");
                return;
            }

            if (ckPushToAtt.Checked)
            {
                task.ProcessTargetHtmlContent(currentPage.PageUrl, currentPage.PageContent);
            }

            updateStatusBarText("done");
        }

        private void updateStatusBarText(string info)
        {
            RDStatusBar.Text = info;
        }

        private void btnOpen_Click(object sender, EventArgs e)
        {
            if (cbRDTaskList.SelectedItem != null)
            { 
                string folderPath = Path.Combine(((RDTaskConfiguration)cbRDTaskList.SelectedItem).ExtendData[DWHConstants.KEY_WEB_ATTACHMENT_STORE_FOLDER],
                    txtURLHashCode.Text);

                if (Directory.Exists(folderPath))
                { 
                    Process.Start(folderPath);
                }
            }
        }
    }
}
